package com.aqie.easy.string;

import java.util.HashMap;
import java.util.Map;

/**
 * 383 字符串全包含
 */
public class CanConstruct {
    /**
     * 1. 哈希表 64ms
     * @param ransomNote
     * @param magazine
     * @return
     */
    public static boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character, Integer> a = new HashMap<>();
        for (Character x : ransomNote.toCharArray()){
            a.put(x, a.getOrDefault(x, 0) + 1);
        }

        HashMap<Character, Integer> b = new HashMap<>();
        for (Character x : magazine.toCharArray()){
            b.put(x, b.getOrDefault(x, 0) + 1);
        }

        System.out.println(a);
        System.out.println(b);
        for (Map.Entry<Character, Integer> entry : a.entrySet()){
            b.put(entry.getKey(), b.getOrDefault(entry.getKey(), 0) - a.get(entry.getKey()));
            if (b.get(entry.getKey()) < 0) return false;
        }
        System.out.println(b);
        return true;
    }

    /**
     * 2.使用数组 5ms
     * @param ransomNote
     * @param magazine
     * @return
     */
    public boolean canConstruct2(String ransomNote, String magazine) {
        int[] ints = new int[26];
        for (char c:magazine.toCharArray()
        ) {
            ints[c-'a']++;
        }
        for (char c:ransomNote.toCharArray()
        ) {
            ints[c-'a']--;
            if(ints[c-'a']<0)return false;
        }
        return true;
    }


    public static void main(String[] args) {
        String a = "abccddd";
        String b = "cabdecd";

        canConstruct(a, b);
    }
}
